﻿Imports System.Data.SqlClient

Public Class frmCorporateProduction

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchClientID.Click
        Dim a As New frmSearchCustID
        a.ShowDialog()

        If SelClientID = 0 Then
            Exit Sub
        End If

        Me.txtClientNo.Text = SelClientID
        Me.txtClientName.Text = SelClientName

        If Me.txtClientName.Text.Trim.Length > 0 Then
            FillClientDetails()
        End If
    End Sub

    Sub FillClientDetails()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("select FullName,Producer From Clients Where SNo=" & Me.txtClientNo.Text, cnn)
            Dim Reader As SqlDataReader

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.txtClientName.Text = Reader.Item("FullName")
                Me.txtProducerSalesman.Text = Reader.Item("Producer")
            End While
            cnn.Close()

            If Me.txtClientName.Text.Trim.Length > 0 Then
                FillPolicies()
            End If

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub txtClientNo_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtClientNo.KeyUp
        If Me.txtClientNo.Text.Trim.Length = 0 Then
            Exit Sub

        ElseIf e.KeyCode = Keys.Enter Then
            FillClientDetails()
        End If
    End Sub

    Private Sub txtClientNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClientNo.TextChanged
        Me.txtClientName.Clear()
        Me.txtProducerSalesman.Clear()
        Me.GridPolicies.Rows.Clear()
    End Sub

    Sub FillInsurCompanies()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim InsurCompanies As ArrayList = GetInsurCompaniesList()

            Me.CombInsurComp.Items.Clear()

            For Each InsurComp As String In InsurCompanies
                Me.CombInsurComp.Items.Add(InsurComp)
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillInsurClasses()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim InsurClasses As ArrayList = GetInsurClassList()

            Me.CombInsurClasses.Items.Clear()

            For Each InsurComp As String In InsurClasses
                Me.CombInsurClasses.Items.Add(InsurComp)
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillPolicies()
        Try
            Me.ErrProv.Clear()
            If Me.txtClientName.Text.Trim.Length = 0 Then
                Me.ErrProv.SetError(Me.txtClientNo, "Please enter a valid client ID")
            Else
                Me.Cursor = Cursors.WaitCursor

                'Check filtering condition
                Dim StrCond As String

                If Me.RInsurComp.Checked = True Then
                    StrCond = "and InsurComp Like N'%" & Me.CombInsurComp.SelectedItem & "%'"
                ElseIf Me.RInsurClass.Checked = True Then
                    StrCond = "and ClassName=N'" & Me.CombInsurClasses.SelectedItem & "'"
                ElseIf Me.RPolicyNo.Checked = True Then
                    StrCond = "and PolicyNo Like N'%" & Me.txtPolicyNo.Text.Trim & "%'"
                ElseIf Me.RAccNo.Checked = True Then
                    StrCond = "and AccNo Like N'%" & Me.txtAccNo.Text.Trim & "%'"
                ElseIf Me.RShowAll.Checked = True Then
                    StrCond = ""
                Else
                    Me.Cursor = Cursors.Default
                    Exit Sub
                End If

                'Check if to show Policies/Endorsements
                If Me.ChEndorsement.Checked = False Then
                    StrCond += " and EndorsType=N'Policy'"
                End If


                'Check Stats (Active, Expired)
                If Me.RActive.Checked = True Then
                    StrCond += " and PeriodTo>=GetDate()"

                ElseIf Me.RExpired.Checked = True Then
                    StrCond += " and PeriodTo<GetDate()"
                End If


                Dim cmd As New SqlCommand("Select SNo,PolEndNo,IsNull(InsurComp,N'') InsurComp,IsNull(ClassName,N'') ClassName," & _
                                          "IsNull(AccNo,N'') AccNo,IsNull(PolicyNo,N'') PolicyNo,IsNull(EndorsNo,N'') EndorsNo," & _
                                          "IsNull(EndorsType,N'Policy') EndorsType," & _
                                          "SumInsur,TotalPremium,NetPremium,CompComm,ProducerComm," & _
                                          "PeriodFrom,PeriodTo,SavedUser,SavedDate,ApprovedUser,ApprovedDate " & _
                                          "From Policies Where ClientNo=" & Me.txtClientNo.Text.Trim & _
                                          " and Done=1 and DoneFin=1 " & StrCond, cnn)
                Dim Reader As SqlDataReader

                Me.GridPolicies.Rows.Clear()

                cnn.Open()
                Reader = cmd.ExecuteReader
                While (Reader.Read)
                    Me.GridPolicies.Rows.Add(New String() {Reader.Item("SNo"), Reader.Item("PolEndNo"), Reader.Item("InsurComp"), _
                                                           Reader.Item("ClassName"), Reader.Item("AccNo"), Reader.Item("PolicyNo"), _
                                                           Reader.Item("EndorsNo"), Reader.Item("EndorsType"), _
                                                           CDbl(Reader.Item("SumInsur")).ToString("N2"), _
                                                           CDbl(Reader.Item("TotalPremium")).ToString("N2"), _
                                                           CDbl(Reader.Item("NetPremium")).ToString("N2"), _
                                                           CDbl(Reader.Item("CompComm")).ToString("N2"), _
                                                           CDbl(Reader.Item("CompComm")).ToString("N2"), _
                                                           CDbl(Reader.Item("ProducerComm")).ToString("N2"), _
                                                           CDbl(Reader.Item("ProducerComm")).ToString("N2"), _
                                                           CDate(Reader.Item("PeriodFrom")).ToString("yyyy/MM/dd"), _
                                                           CDate(Reader.Item("PeriodTo")).ToString("yyyy/MM/dd"), _
                                                           Reader.Item("SavedUser"), _
                                                           CDate(Reader.Item("SavedDate")).ToString("yyyy/MM/dd"), _
                                                           Reader.Item("ApprovedUser"), _
                                                           CDate(Reader.Item("ApprovedDate")).ToString("yyyy/MM/dd"), "Edit", "Delete"})
                End While
                cnn.Close()

                'Count no. of results
                Me.lblResult.Text = "Results found: " & Me.GridPolicies.Rows.Count

                'Check Color (Make red if Endors.)
                For Each row As DataGridViewRow In Me.GridPolicies.Rows
                    If row.Cells(Me.DGColType.Index).Value = "Deletion" Then
                        row.Cells(Me.DGColType.Index).Style.ForeColor = Color.Red
                        row.Cells(Me.DGColSumInsured.Index).Style.ForeColor = Color.Red
                        row.Cells(Me.DGColTotPremium.Index).Style.ForeColor = Color.Red
                        row.Cells(Me.DGColNetPremium.Index).Style.ForeColor = Color.Red
                        row.Cells(Me.DGColCompCommPerc.Index).Style.ForeColor = Color.Red
                        row.Cells(Me.DGColCompComm.Index).Style.ForeColor = Color.Red
                        row.Cells(Me.DGColProdCommPerc.Index).Style.ForeColor = Color.Red
                        row.Cells(Me.DGColProdComm.Index).Style.ForeColor = Color.Red
                    End If
                Next

                Me.Cursor = Cursors.Default
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If Me.txtClientName.Text.Trim.Length = 0 Then
            Exit Sub
        Else
            Try
                Me.Cursor = Cursors.WaitCursor
                Dim dap As New SqlDataAdapter("Select PESno,PESSNo,ClientNo,dbo.GetClientName(ClientNo) ClientName,AccNo,PolicyNo,EndorsNo," & _
                                              "EndorsType,InsurComp,IssueDate,ClassName,InterestInsur,PeriodFrom," & _
                                              "PeriodTo,SumInsur,TotalPremium,NetPremium,CompComm,ProducerComm,TransDate,SavedDate," & _
                                              "SavedUser,Department From Policies Where ClientNo=" & Me.txtClientNo.Text.Trim & _
                                              " and Done=1", cnn)
                Dim das As New DataSet
                das.Clear()

                cnn.Open()
                dap.Fill(das, "Policies")
                cnn.Close()

                Dim rpt As New ClientsPolicies
                rpt.SetDataSource(das)
                rptViewer.CrystalReportViewer1.ReportSource = rpt
                rptViewer.CrystalReportViewer1.RefreshReport()
                rptViewer.ShowDialog()

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If Me.txtClientName.Text.Trim.Length = 0 Then
            MsgBox("Please enter a valid client no.")
            Me.txtClientNo.Focus()
            Exit Sub
        Else
            Dim a As New frmPolicyMngNew
            a.txtClientNo.Text = Me.txtClientNo.Text.Trim
            a.txtClientName.Text = Me.txtClientName.Text
            a.FillBranches()

            If Me.txtProducerSalesman.Text = "" OrElse Me.txtProducerSalesman.Text.Contains("Direct Business - ") Then
                a.CombDirectBusiness.SelectedItem = Me.txtProducerSalesman.Text
                a.RDirectBusiness.Checked = True
            Else
                a.RProducer.Checked = True
            End If
            a.ShowDialog()

            FillPolicies()
        End If
    End Sub

    Private Sub FrmPolicyMng_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized

        FillInsurClasses()
        FillInsurCompanies()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FillPolicies()
    End Sub

    Sub CheckRadio()
        If Me.RInsurComp.Checked = True Then
            Me.CombInsurComp.Enabled = True
            Me.CombInsurClasses.Enabled = False
            Me.txtPolicyNo.Enabled = False
            Me.txtAccNo.Enabled = False

        ElseIf Me.RInsurClass.Checked = True Then
            Me.CombInsurComp.Enabled = False
            Me.CombInsurClasses.Enabled = True
            Me.txtPolicyNo.Enabled = False
            Me.txtAccNo.Enabled = False

        ElseIf Me.RPolicyNo.Checked = True Then
            Me.CombInsurComp.Enabled = False
            Me.CombInsurClasses.Enabled = False
            Me.txtPolicyNo.Enabled = True
            Me.txtAccNo.Enabled = False

        ElseIf Me.RAccNo.Checked = True Then
            Me.CombInsurComp.Enabled = False
            Me.CombInsurClasses.Enabled = False
            Me.txtPolicyNo.Enabled = False
            Me.txtAccNo.Enabled = True

        ElseIf Me.RShowAll.Checked = True Then
            Me.CombInsurComp.Enabled = False
            Me.CombInsurClasses.Enabled = False
            Me.txtPolicyNo.Enabled = False
            Me.txtAccNo.Enabled = False
            FillPolicies()
        End If
    End Sub

    Private Sub RInsurComp_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RInsurComp.CheckedChanged
        CheckRadio()
    End Sub

    Private Sub RInsurClass_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RInsurClass.CheckedChanged
        CheckRadio()
    End Sub

    Private Sub RPolicyNo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RPolicyNo.CheckedChanged
        CheckRadio()
    End Sub

    Private Sub RAccNo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RAccNo.CheckedChanged
        CheckRadio()
    End Sub

    Private Sub RShowAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RShowAll.CheckedChanged
        CheckRadio()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub GridPolicies_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridPolicies.CellClick
        If e.ColumnIndex = Me.DGColDocuments.Index Then
            Dim a As New frmPoliciesDocuments
            a.TransNo = CInt(Me.GridPolicies.CurrentRow.Cells(Me.DGColSNo.Index).Value)
            a.ShowDialog()
        End If
    End Sub
End Class